#!/bin/bash

# 自动导入全国行政区划数据脚本
echo "开始自动导入全国行政区划数据..."

# 进入MySQL容器并执行导入
docker exec -i mysql-5.7 mysql -uroot -p'abc#$123' smart_community << 'EOF'

-- 清空现有区域数据（可选，谨慎使用）
-- DELETE FROM sc_region WHERE region_code != '86';

-- 导入所有省份数据
source /tmp/province_sqls/province_11_北京市.sql;
source /tmp/province_sqls/province_12_天津市.sql;
source /tmp/province_sqls/province_13_河北省.sql;
source /tmp/province_sqls/province_14_山西省.sql;
source /tmp/province_sqls/province_15_内蒙古自治区.sql;
source /tmp/province_sqls/province_21_辽宁省.sql;
source /tmp/province_sqls/province_22_吉林省.sql;
source /tmp/province_sqls/province_23_黑龙江省.sql;
source /tmp/province_sqls/province_31_上海市.sql;
source /tmp/province_sqls/province_32_江苏省.sql;
source /tmp/province_sqls/province_33_浙江省.sql;
source /tmp/province_sqls/province_34_安徽省.sql;
source /tmp/province_sqls/province_35_福建省.sql;
source /tmp/province_sqls/province_36_江西省.sql;
source /tmp/province_sqls/province_37_山东省.sql;
source /tmp/province_sqls/province_41_河南省.sql;
source /tmp/province_sqls/province_42_湖北省.sql;
source /tmp/province_sqls/province_43_湖南省.sql;
source /tmp/province_sqls/province_44_广东省.sql;
source /tmp/province_sqls/province_45_广西壮族自治区.sql;
source /tmp/province_sqls/province_46_海南省.sql;
source /tmp/province_sqls/province_50_重庆市.sql;
source /tmp/province_sqls/province_51_四川省.sql;
source /tmp/province_sqls/province_52_贵州省.sql;
source /tmp/province_sqls/province_53_云南省.sql;
source /tmp/province_sqls/province_54_西藏自治区.sql;
source /tmp/province_sqls/province_61_陕西省.sql;
source /tmp/province_sqls/province_62_甘肃省.sql;
source /tmp/province_sqls/province_63_青海省.sql;
source /tmp/province_sqls/province_64_宁夏回族自治区.sql;
source /tmp/province_sqls/province_65_新疆维吾尔自治区.sql;

-- 显示导入结果统计
SELECT 
    region_type,
    COUNT(*) as count,
    CASE region_type 
        WHEN 1 THEN '省级'
        WHEN 2 THEN '市级' 
        WHEN 3 THEN '区县级'
        WHEN 4 THEN '街道级'
        WHEN 5 THEN '社区级'
        ELSE '其他'
    END as type_name
FROM sc_region 
GROUP BY region_type 
ORDER BY region_type;

SELECT '导入完成！' as message;

EOF

echo "全国行政区划数据导入完成！" 